/**
 * Created by Administrator on 2015/6/1.
 */
var express = require('express');
var router = express.Router();
var auth = require('../middlewares/auth');
var passport = require('passport')
    , LocalStrategy = require('passport-local').Strategy;

//获取用户的工具类
var userServices = require('../services/User');
var articleServices = require('../services/Article');
var categoryServices = require('../services/Category');

String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

router.all(/^\/(?!login|register).*$/, auth.isAdmin);
router.get('/', function (req, res) {
    res.render('admin/index', {nicename: req.user.nicename || "", activeList: ['a-index']});
});

router.get('/index', function (req, res) {
    res.redirect('/admin/');
})

/** --------------------------------------------------------------------------------------------
 *  处理登录相关请求
 * */
router.route('/login')
    .get(function (req, res) {
        res.render('admin/login', {});
    })
    .post(passport.authenticate('local', {
        successRedirect: '/admin/',
        failureRedirect: '/admin/login'
    }))
;

/** --------------------------------------------------------------------------------------------
 *  退出
 * */
router.get('/logout', function (req, res) {
    req.logout();
    res.redirect('/admin/login');
});

/** --------------------------------------------------------------------------------------------
 *  注册
 * */
router.post('/register', function (req, res) {
    var username = req.body.username.trim();
    var password = req.body.password.trim();
    var nicename = req.body.nicename.trim();
    var email = req.body.email.trim();
    var key = req.body.key.trim();

    //如果没有邀请码，或邀请码不正确，则不通过注册
    if ('gxdfbd599A' !== key) {
        res.statusCode = 500;
        res.send({result: "邀请码不合格"});
    } else {

        userServices.register(username, password, nicename, email, function (user) {

            if (!user) {
                res.statusCode = 500;
            } else {
                res.statusCode = 200;
            }

            var result = user ? user : "注册失败";
            res.send({result: result});
        })
    }


});


router.get('/articles/', function (req, res) {
    res.render('admin/articles_list', {nicename: req.user.nicename || "", activeList: ['a-articles','li-articles-list']});
})

/** -------------------------------------------------------------------------------------
 *  进入技术应用编辑页面
 *  -------------------------------------------------------------------------------------
 * */
router.get('/articles/editpage/', function (req, res) {
    var nicename = req.user.nicename || "";

    res.render('admin/articles_edit', {_id: null, nicename: nicename, activeList: ['a-articles', 'li-articles-editpage']});
})

router.get('/articles/editpage/:_id', function (req, res) {
    var _id = req.params._id || "";
    var nicename = req.user.nicename || "";

    res.render('admin/articles_edit', {_id: _id, nicename: nicename, activeList: ['a-articles', 'li-articles-editpage']});
});

/** -------------------------------------------------------------------------------------
 *  创建、修改、获取、删除文章
 *  -------------------------------------------------------------------------------------
 */
router.route('/articles/content/')
    .post(function (req, res) {
        var article = req.body.article;

        if (!article) {
            res.statusCode = 200;
            res.send({result: "error"});
        } else {

            articleServices.insert(article, function (result) {
                if (result === "success") {
                    res.statusCode = 200;
                } else {
                    res.statusCode = 500;
                }

                res.send({result: result});
            });
        }


    })
    .put(function (req, res) {

        var article = req.body.article;
        if (!article) {
            res.statusCode = 200;
            res.send({result: "error"});
        } else {

            articleServices.update(article, function (result) {
                if (result === "success") {
                    res.statusCode = 200;
                } else {
                    res.statusCode = 500;
                }
                res.send({result: result});
            })
        }

    })
    .delete(function (req, res) {

        var _idList = req.body._idList;
        if (!_idList || _idList.length == 0) {
            res.statusCode = 500;
            res.send({result: "error"});
        } else {

            articleServices.delete(_idList, function (result) {
                if (result === "success") {
                    res.statusCode = 200;
                } else {
                    res.statusCode = 500;
                }

                res.send({result: result});
            })
        }

    })
;


//---------------------------------------------------------------------------------------------------
//  Product

router.get('/products/list', function (req, res) {
    res.render('admin/products_list', {nicename: req.user.nicename || "", activeList: ['a-products','li-products-list']});
});
router.get('/products/editpage', function (req, res) {
    var nicename = req.user.nicename || "";

    res.render('admin/products_edit', {nicename: nicename, activeList: ['a-products', 'li-products-editpage']});
});
router.get('/products/editpage/:_id', function (req, res) {
    var _id = req.params._id || "";
    var nicename = req.user.nicename || "";

    res.render('admin/products_edit', {_id: _id, nicename: nicename, activeList: ['a-products', 'li-products-editpage']});
});

module.exports = router;
